package com.bbms.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bbms.dao.ReaderDao;
import com.bbms.entity.Page;
import com.bbms.entity.Reader;
import com.bbms.entity.User;
import com.bbms.service.ReaderService;
import com.bbms.service.UserService;

@Service("ReaderServiceImpl")
public class ReaderServiceImpl implements ReaderService{

	
	@Resource(name="ReaderDaoImpl")
    private ReaderDao rd;
	@Resource(name="UserServiceImpl")
	private UserService us;
	
	/*
	 * 添加读者
	 * @see com.bbms.service.BaseService#add(java.lang.Object)
	 */
	@Override
	public void add(Reader t) {
		
	}

	/*
	 *更新读者信息
	 * @see com.bbms.service.BaseService#update(java.lang.Object)
	 */
	@Override
	public void update(Reader t) {
		
	}

	/*
	 * 删除单个读者
	 * @see com.bbms.service.BaseService#delete(java.lang.Object)
	 */
	@Override
	public void delete(Reader t) {
		
	}

	/*
	 * 保存两个对象
	 * @see com.bbms.service.ReaderService#saveTwo(com.bbms.entity.Reader, com.bbms.entity.User)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void saveTwoObj(Reader reader,User user) {
		
		Integer userId;
		us.add(user);
		List<HashMap> list=(List<HashMap>)us.queryByAccount(user.getAccount());
		userId=Integer.parseInt(list.get(0).get("id").toString());
		reader.setUserInfoId(userId);
		rd.save(reader);
	}
	
	/*
	 * 更新两个对象
	 * @see com.bbms.service.ReaderService#updateTwoObj(com.bbms.entity.Reader, com.bbms.entity.User)
	 */
	@Override
	public void updateTwoObj(Reader reader, User user) {
		
		System.out.println("更改：==============================");
		System.out.println("读者："+ reader.toString());
		System.out.println("用户："+ user.toString());
		System.out.println("==============================");
		
		int userId=reader.getUserInfoId();
		user.setId(userId);
		rd.update(reader);
		us.update(user);
		
	}
	
	/*
	 * 同时删除两个对象
	 * @see com.bbms.service.ReaderService#delTwoObj(com.bbms.entity.Reader, com.bbms.entity.User)
	 */
	@Override
	public void delTwoObj(Reader reader, User user) {
		
		System.out.println("删除：==============================");
		System.out.println("读者："+ reader.toString());
		System.out.println("用户："+ user.toString());
		System.out.println("==============================");
		
		int userId=reader.getUserInfoId();
		user.setId(userId);
		rd.delete(reader);
		us.delete(user);
	}
	
	
	/*
	 * 获取分页数据(无查询)
	 * @see com.bbms.dao.BaseDAO#getPage(int, int)
	 */
	@Override
	public Page getPage(int currentPage, int pageSize) {
		Page page=new Page();
		page=rd.getPage(currentPage, pageSize);
		return page;
	}

	/*
	 * 获取分页数据（查询）
	 * @param currentPage 当前页
	 * @param pageSize 数目
	 * @param s_name 读者姓名
	 * @param s_account 读者账号
	 * @param s_phone 读者电话
	 * @param s_categoryId 读者类别
	 * @param IDCard 读者身份证
	 * @param start_time 开始时间
	 * @param end_time 结束时间
	 * @return 分页数据
	 * @see com.bbms.service.ReaderService#getPage(int, int, java.lang.String, java.lang.String, java.lang.String, java.lang.Integer, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public Page getPage(int currentPage, int pageSize, String s_name, String s_account, String s_phone,
			Integer s_categoryId, String IDCard, String start_time, String end_time,  Integer endTime) {
		Page page=new Page();
		page=rd.getPage(currentPage, pageSize, s_name, s_account, s_phone, s_categoryId, IDCard, start_time, end_time, endTime);
		return page;
	}

	/*
	 * 批量删除读者
	 * @see com.bbms.service.ReaderService#bacthDel(java.util.List, java.lang.String)
	 */
	@SuppressWarnings({ "rawtypes"})
	@Override
	public boolean bacthDel(List<Integer> list, String password, String managerAccount) {
		
		Reader reader=new Reader();
		User user=new User();
		
		List<?> managerList=rd.findCurrentManager(managerAccount, password);
		
		System.out.println("当前管理员：   "+managerList.toString());
		
		if (managerList.size()>0) {
			
			for(int i=0; i<list.size(); i++){
				
				int readerId=Integer.parseInt(list.get(i).toString());
				List<HashMap> userList=rd.findUserByReaderId(readerId);
				int userId=Integer.parseInt(userList.get(0).get("userId").toString());
				reader.setId(readerId);
				user.setId(userId);
	
				System.out.println("读者： "+ reader.toString() +"===================");
				System.out.println("用户： "+ user.toString() +"===================");
				
				rd.delete(reader);
				us.delete(user);
			}
			
			return true;
		}
		
		return false;
	}


}
